/**
 * @fileoverview
 * This script defines utilities for interacting with data from www.fly-ted.org.
 * @author <a href="http://purl.org/net/aliman">Alistair Miles</a>, Graham Klyne, Jun Zhao
 * @version $Revision:538 $ on $Date: 2008-08-27 09:08:41 +0100 (Wed, 27 Aug 2008) $ by $Author: aliman $
 * @requires YAHOO.util.Connect
 * @requires flyui.bdgp.GeneNameMappings
 * TODO license terms
 */

// create a namespace if not already defined
flyui.namespace("flyui.bdgp");

/**
 * TODO doc me
 */
flyui.bdgp.Service = function (endpointURL) {
	/**
	 * @private
	 */
	this._endpointURL = endpointURL;
}



/**
 * TODO doc me
 */
flyui.bdgp.Service.prototype.query = function (query, success, failure){
	log("flyui.bdgp.Service :: query request: "+query);
	
	// define the callback object
	var callback = {
		success: success,
		failure: failure
	};
			
	var url = this._buildURL(query);
	
	log("flyui.flybase.Service :: make the request to "+url);
	var request = YAHOO.util.Connect.asyncRequest("GET", url, callback, null);
	
};


flyui.bdgp.Service.prototype.findImagesByAnyGeneLabel = function( geneLabel, success, failure) {
	log("flyui.bdgp.Service :: findImagesByAnyGeneLabel: "+geneLabel);
	
	var successAdapter = flyui.bdgp.Service._buildSuccessAdapterForFindImagesByAnyGeneLabel(success);

	var query = flyui.bdgp.Service._buildQueryForFindImagesByAnyGeneLabel(geneLabel);
	
	this.query(query, successAdapter, failure);	
}


/**
 * Construct the request URL for the given SPARQL query.
 * @private
 * @param {String} query a SPARQL query
 * @return a request URL
 * @type String
 */
flyui.bdgp.Service.prototype._buildURL = function( query ) {
	return this._endpointURL + "?output=json&query=" + escape(query);
};



/**
 * Return a success case request handler for the findImagesByAnyGeneLabel method.
 * @param {Function} adaptedSuccess the function to call with an array of Image objects
 * @return a success callback to use in the HTTP request
 * @type Function
 * @private
 * @static
 */
flyui.bdgp.Service._buildSuccessAdapterForFindImagesByAnyGeneLabel = function( adaptedSuccess ) {

	return function(responseObject) {
		
		log("response received: "+responseObject.status, "debug");
		
		// try parsing response text as json
		log("parsing response: "+responseObject.responseText, "debug");
		var resultSet = YAHOO.lang.JSON.parse(responseObject.responseText);
		
		// convert result set to an array of genes
		var images = flyui.bdgp.Image.newInstancesFromSPARQLResults(resultSet);
		
		// complete the request
		adaptedSuccess(images);
	}

};



/**
 * Build a SPARQL query to retrieve images by bdgp gene name.
 * @param {String} anyName the gene name to search by
 * @return a SPARQL query
 * @type String
 * @private
 * @static
 */
flyui.bdgp.Service._buildQueryForFindImagesByAnyGeneLabel = function( geneLabel ) {

	var prefixes = 	"PREFIX bdgp: <http://purl.org/net/bdgp/schema/>  " +
					"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
					"PREFIX skos: <http://www.w3.org/2008/05/skos#> " +
					"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
					"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +		
					"PREFIX : <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#> " +
					"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
					"PREFIX dc: <http://purl.org/dc/elements/1.1/> ";					

//	var body = 		"SELECT DISTINCT ?thumbnailURL WHERE { " +
//						"?thumbnailURL digitalmedia:depicts [ oban:has_participants_from [ skos:altLabel '" + geneLabel + "' ] ] " +
//					"}";

	var body = "select distinct ?imagepath where {" +
				"?association bdgp:associate ?evidence; bdgp:geneProduct ?gp ." +
				"?evidence bdgp:evidence ?img ." +
				"?img rdf:type bdgp:Image ; bdgp:imagePath ?imagepath." +
				"?gp skos:altLabel '" + geneLabel + "'; rdf:type bdgp:GeneProduct ." +
				"}";
						
	var query = prefixes + body;

	return query;
};


/**
 * TODO doc me
 */
flyui.bdgp.Image = function() {

	/**
	 * @type String
	 */
	this.fullImageURL = null;
	
	/**
	 * @type String
	 */
	this.thumbnailURL = null;

} 


/**
 * TODO doc me
 */
flyui.bdgp.Image.newInstancesFromSPARQLResults = function( resultSet ) {

	var postProcess = function(image) {
		var imagePath = image.imagepath;
		
		image.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/" + imagePath;
		image.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/" + imagePath;
		
	};
	
	var bindings = resultSet.results.bindings;
	var images = [];
	for (var i=0; i<bindings.length; i++) {
		var binding = bindings[i];
		var image = {};
		for (var variable in binding) {
			image[variable] = binding[variable]["value"];
		}
		postProcess(image);
		images[images.length] = image;
	}
	return images;
}


